package data.mediator;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import data.dao.DBManager;
import data.dao.IEventDAO;
import data.dao.ITruckDAO;;
import data.model.Truck;
import data.model.Event;


public class DBMediator<Type> {
    private static final Logger LOGGER = Logger.getLogger(DBMediator.class.getName());
    
    public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy");
    
    public DBMediator(){}
  
    /*__________________________Truck____________________*/


    public static List getTrucks() {
	List<Truck> trucks = DBManager.INSTANCE.getTruckDAO().getTrucks();	
	return trucks;     
    }
    
    
    public static Truck getTruck(int id) {
	Truck truck = DBManager.INSTANCE.getTruckDAO().getTruck(id);	
	return truck;
    }
    
  	
    public static boolean addOrModifyTruck(Truck truck) {
	LOGGER.log(Level.INFO, "Adding or modifying Truck element");
	
	boolean success;

	success = DBManager.INSTANCE.getTruckDAO().addOrModify(truck);
	    
	
	//LOGGER.log(Level.INFO, "Problem while adding or modifying", e);
	//   success = false;
	
	return success;
    }
    


    /*_________________________Event_______________________*/
    
    public static List getEvents() {
	List<Event> events = DBManager.INSTANCE.getEventDAO().getEvents();	
	return events;
    }
  
    public static Event getEvent(int id) {
	Event event = DBManager.INSTANCE.getEventDAO().getEvent(id);	
	return event;
    }
    
    
    public static boolean addOrModifyEvent(Event event) {
	boolean success = true;
	LOGGER.log(Level.INFO, "Adding Event element: ");
	if (success) {
	    // on rajoute les utilisateurs au passage
	    DBManager.INSTANCE.getTruckDAO().addOrModify(new Truck(event.getTruck()));
	}	
	
	return DBManager.INSTANCE.getEventDAO().addOrModify(event);
    }
    
      
    /*  public static boolean parseInitDB(String initDBPath) {
	LOGGER.log(Level.INFO, "Parsing init DB file: " + initDBPath);
	
	String initDBStream;
	try {
	    initDBStream = ServletToolkit.readFile(initDBPath);
	} catch (IOException e) {
	    LOGGER.log(Level.SEVERE, "Error while parsing init DB file");
	    initDBStream = null;
	}
	
	if (initDBStream != null) {
	    return importXMLTrucks(initDBStream);
	} else {
	    return false;
	}
	} */ 
}
